package com.gome.ocean.controller.authority;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.Page;
import com.gome.ocean.common.utils.ResponsePage;
import com.gome.ocean.dao.model.authority.Module;
import com.gome.ocean.service.authority.ModuleService;
import com.gome.ocean.service.base.BaseController;

/**
 * @author zhangguoqing
 * @date 2016.6.29
 * @param 模块控制管理层 用户权限中对于模块功能的CURD管理
 */
@Controller
@RequestMapping("/module")
public class ModuleController extends BaseController{
	
	private static final Logger LOG = LoggerFactory.getLogger(ModuleController.class);
	
	@Autowired
	private ModuleService 		moduleService;
	
	@RequestMapping(value = "/toInsertModule" , method = RequestMethod.GET)
	public ModelAndView toInsertModule(ModelAndView mav){
		mav.setViewName("authority/module/module-insert");
		return mav;
	}
	
	@RequestMapping(value = "/toAjaxInsertModule" , method = RequestMethod.POST)
	@ResponseBody
	public ResponsePage toAjaxInerstModule(){
		ResponsePage page = new ResponsePage();
		String name = getParamValue("name");
		if (StringUtils.isBlank(name)) {
			return page.setErrorMsg("请输入模块名称");
		}
		String desc = getParamValue("descriptor");
		if (StringUtils.isBlank(desc)) {
			return page.setErrorMsg("请对模块 进行简单描述，方便维护");
		}
		Module module = new Module();
		module.setName(name);
		module.setDescriptor(desc);
		try {
			if (moduleService.insertModule(module)) {
				return page.setSuccessMsg("新建模块成功");
			}
			return page.setErrorMsg("新建失败");
		} catch (Exception e) {
			return page.setErrorMsg("异常信息："+e.toString());
		}
	}
	
	@RequestMapping(value="/toFindModule" , method = RequestMethod.GET)
	public ModelAndView toFindModule(ModelAndView mav){
		mav.setViewName("/authority/module/module-find");
		return mav;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/toAjaxFindModule", method = RequestMethod.POST)
	@ResponseBody
	public ResponsePage toAjaxFindModule(HttpServletRequest req){
		ResponsePage res = new ResponsePage();
		int pageNum = getPageNum();
        int pageSize = getPageSize();
        String name = req.getParameter("name");
        Map<String, Object> map = new HashMap<String,Object>();
        try {
        	if (StringUtils.isBlank(name)) {
        		Page<Module> modulePage = moduleService.findAllModule(pageNum, pageSize);
    			if (modulePage!=null && modulePage.size()>0) {
    				return res.setPage(modulePage, "查询成功");
    			}
			}else{
				map.put("name", name);
				Module module = moduleService.findModuleByMap(map);
				Page<Module> page = new Page<Module>();
				page.add(module);
				page.setTotal(1);
				if (module != null) {
					return res.setPage(page, "查询成功");
				}
			}
			
			return res.setSuccessMsg("没有数据");
		} catch (Exception e) {
			LOG.error(e.toString());
			return res.setErrorMsg(e.toString());
		}
	}
	
	@RequestMapping(value="/remove" , method=RequestMethod.POST)
	@ResponseBody
	public ResponsePage toAjaxRemove(){
		ResponsePage response = new ResponsePage();
		String id = getParamValue("id");
		try {
			boolean flag = moduleService.removeModuleById(Long.parseLong(id));
			if (flag) {
				return response.setSuccessMsg("删除成功");
			}
			return response.setErrorMsg("删除失败");
		} catch (Exception e) {
			LOG.error(e.toString());
			return response.setErrorMsg("删除异常信息：" + e.toString());
		}
		
	}
	
	@RequestMapping(value = "/update" , method = RequestMethod.POST)
	@ResponseBody
	public ResponsePage toAjaxUpdate(){
		ResponsePage response = new ResponsePage();
		String name = getParamValue("name");
		if (StringUtils.isBlank(name)) {
			return response.setErrorMsg("模块名称不能为空");
		}
		String desc = getParamValue("descriptor");
		if (StringUtils.isBlank(desc)) {
			return response.setErrorMsg("描述信息不能为空");
		}
		String id = getParamValue("id");
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			map.put("name", name);
			Module module = moduleService.findModuleByMap(map);
			if (module!=null &&  !module.getId().equals(Long.parseLong(id))) {
				return response.setErrorMsg("模块名称存在");
			}
			Module moduleBo = new Module();
			moduleBo.setId(Long.parseLong(id));
			moduleBo.setName(name);
			moduleBo.setDescriptor(desc);
			if (moduleService.updateModule(moduleBo)) {
				return response.setSuccessMsg("修改成功");
			}
			return response.setErrorMsg("修改失败");
		} catch (Exception e) {
			LOG.error(e.toString());
			return response.setErrorMsg("异常信息:"+ e.toString());
		}
		
	}
	
}
